#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import <LockInfo/Plugin.h>

#import "framework/FWCommon.h"
#import "framework/FWDetailListEntry.h"
#import "framework/FWDetailListSection.h"
#import "framework/FWPlugin.h"

#import "PICommon.h"
#import "PIEventDebug.h"
#import "PIAlarm.h"
#import "PIEvent.h"
#import "PIEventStatus.h"
#import "PIContext.h"
#import "PIEventPlugin.h"


@implementation PIEventDebug

@synthesize alarms, events;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithAlarms:(NSArray*)newAlarms events:(NSArray*)newEvents
{
    if ((self = [super init])) {
        self.alarms = newAlarms;
        self.events = newEvents;
    }
    
    return self;
}

- (void)dealloc
{
    [alarms release];
    [events release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Accessing records from the database

- (NSArray*)detailSectionsForListPage:(NSObject *)page key:(NSObject*)key application:(FWApplication *)application
{  
    NSMutableArray* alarmSect = [NSMutableArray arrayWithCapacity:alarms.count];
    
    for (PIAlarm* alarm in alarms) {
        PIEvent* event = [events piObjectForKey:alarm.key];
        
        NSString* type;
        switch (alarm.type) {
            case kPIEventAlarmTypeNative:
                type = @"Native";
                break;
            case kPIEventAlarmTypeExtraAlarm:
                type = @"Extra Alarm at";
                break;
            case kPIEventAlarmTypeExtraReminder:
                type = @"Extra Remind at";
                break;
            case kPIEventAlarmTypeAlarmIsActive:
                type = @"Alarm Active until";
                break;
            default:
                type = @"??";
                break;
        }

        NSString* name = [event title];
        if (!name)
            name = alarm.key.length > 20 ? [alarm.key substringFromIndex:alarm.key.length - 20] : alarm.key;
        
        [alarmSect addObject:[[[FWDetailListEntry entryWithTitle:[NSString stringWithFormat:
                                                                  @"Key: %@\n"
                                                                  "Title: %@\n\n"
                                                                  "Status: %@ %@",
                                                                  alarm.key,
                                                                  name,
                                                                  type,
                                                                  FWDebugTimeStringForUTCDate(alarm.date)]]
                               withSize:[UIFont systemFontSize]]
                              withNoBold]];
    }
    

    PIEventStatus* info = (PIEventStatus*)application.info;
    PIEventPlugin* cal = (PIEventPlugin*)application.fwPlugin;

    NSArray* datesSect = [NSArray arrayWithObjects:
                          [FWDetailListEntry entryWithTitle:@"⚡ Next Change Due" value:FWDebugTimeStringForUTCDate(info.nextChangeDue)],
                          [FWDetailListEntry entryWithTitle:@"🔔 Next Alarm Due" value:FWDebugTimeStringForUTCDate(info.nextAlarmDue)],
                          [FWDetailListEntry entryWithTitle:@"🌙 Mute Alarm Until" value:FWDebugTimeStringForUTCDate(cal.muteUntilDate)],
                          [FWDetailListEntry entryWithTitle:@"⚠ Next Activity Due"
                                                      value:FWDebugTimeStringForUTCDate(application.fwPlugin.nextScheduledActivity)],
                          nil];
                           

    if (alarmSect.count > 0)
        return [NSArray arrayWithObjects:
                [FWDetailListSection sectionForElements:datesSect withTitle:@"Next Action Due"],
                [FWDetailListSection sectionForElements:alarmSect withTitle:@"Alarms"],
                nil];
    else
        return [NSArray arrayWithObject:
                [FWDetailListSection sectionForElements:datesSect withTitle:@"Next Action Due"]];
}

@end
